/*
 * term_vt100.c
 *
 *  Created on: 16-07-2013
 *      Author: MCzepczyński
 */

/******************************************************************************************
 *	Deklaracje dołączanych plików nagłówkowych i bibliotek								 *
 *****************************************************************************************/
#include "term_vt100.h"

const char CLS[] PROGMEM = { "\x1b""[2J" };
const char HOME[] PROGMEM = { "\x1b""[;H" };
const char CURSOR_HIDE[] PROGMEM = { "\x1b""[?25l" };
const char CURSOR_SHOW[] PROGMEM = { "\x1b""[?25h" };
const char ATTRIBUTE_OFF[] PROGMEM = { "\x1b""[m" };

/******************************************************************************************
 *	Deklaracje funkcji															 		 *
 *****************************************************************************************/

/*
 * 	Funkcja wyswietlajaca kursor w terminalu
 *  Argument: 	0 - kursor wlaczony
 *         		1 - kursor wylaczony
 */
void term_cursor_hide(uint8_t hide) {
	if(hide) uart_write_text_P(CURSOR_HIDE);
	else uart_write_text_P(CURSOR_SHOW);
}/* term_cursor_hide */

/*
 * 	Funkcja czyszczaca terminal
 */
void term_clear(void){
	uart_write_text_P(ATTRIBUTE_OFF);
	uart_write_text_P(CLS);
	uart_write_text_P(HOME);
}/* term_clear */

/*
 * 	Funkcja rysujaca linie
 *  Argument: 	sign - znak do rysowania
 *  			cnt - dlugosc linii
 *  			font_color - kolor linii
 *  			bg_color - kolor tla
 */
void term_draw_line(char sign, uint8_t cnt, uint8_t font_color, uint8_t bg_color){
	term_set_attributes(0, font_color, bg_color);

	for(uint8_t i=0; i<cnt; i++)
		uart_write_char(sign);
}/* term_draw_line */

/*
 * 	Funkcja ustawiajaca atrybuty pisania
 *  Argument: 	attribute - atrybut, np. BOLD, BLINK ...
 *  			font_color - kolor czcionki
 *  			bg_color - kolor tla
 */
void term_set_attributes(uint8_t attribute, uint8_t font_color, uint8_t bg_color){	// Kod: <ESC>[{attribute};3{font_color};4{bg_color}m
	uart_write_char(0x1B);
	uart_write_char('[');
	uart_write_char(attribute + '0');
	uart_write_char(';');
	uart_write_char('3');
	uart_write_char(font_color + '0');
	uart_write_char(';');
	uart_write_char('4');
	uart_write_char(bg_color + '0');
	uart_write_char('m');
}/* term_set_attributes */

/*
 * 	Funkcja ustawiajaca kolor czcionki
 *  Argument: 	font_color - kolor czcionki
 */
void term_font_color( uint8_t font_color ) {	// Kod: <ESC>[3{font_color}m
	uart_write_char(0x1B);
	uart_write_char('[');
	uart_write_char('3');
	uart_write_char(font_color + '0');
	uart_write_char('m');
}/* term_font_color */

/*
 * 	Funkcja ustawiajaca kolor tla
 *  Argument: 	bg_color - kolor tla
 */
void term_bg_color(uint8_t bg_color){		// Kod: <ESC>[4{bg_color}m
	uart_write_char(0x1B);
	uart_write_char('[');
	uart_write_char('4');
	uart_write_char(bg_color + '0');
	uart_write_char('m');
}/* term_bg_color */

/*
 * 	Funkcja ustawiajaca kursor w polozeniu x,y na terminalu
 *  Argument: 	x - wspolrzedna x kursora
 *         		y - wspolrzedna y kursora
 */
void term_go_to(uint8_t x, uint8_t y){		// Kod: <ESC>[{y};{x}H
	uart_write_char(0x1B);
	uart_write_char('[');
	uart_write_int(y, 0);
	uart_write_char(';');
	uart_write_int(x, 0);
	uart_write_char('H');
}/* term_locate */
